<template>
  <a-modal
    title="订单物料"
    :width="1200"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :footer="null"
    @ok="handleSubmit"
    @cancel="handleCancel">

    <a-card :bordered="false">

      <s-table
        ref="table"
        size="default"
        :columns="columns"
        :data="loadData"
        :alert="true"
        :rowKey="(record) => record.code"
        :showPagination="false"
      >

        <span slot="action" slot-scope="text, record">
            <a @click="$refs.deliverDetail.show(record)">发货明细</a>
        </span>
        <span slot="materialModelNumber" slot-scope="text, record">
            {{record.material?record.material.materialModelNumber:''}}
        </span>

      </s-table>

      <deliver-detail ref="deliverDetail"/>

    </a-card>
  </a-modal>
</template>

<script>
import {STable} from '@/components'
import {page} from '@/api/modular/main/emsordermaterial/emsOrderMaterialManage'
import DeliverDetail from '@/views/main/purchaseCenter/rawMaterial/purchaseOrderByWaitingDeliver/deliverDetail'

export default {
  components: {
    STable,
    DeliverDetail,
  },
  data() {
    return {
      title: null,
      advanced: false, // 高级搜索 展开/关闭
      queryParam: {},  // 查询参数
      columns: [
        {
          title: '序号', width:'80px',
          dataIndex: 'index',
          customRender: (text, record, index) => `${index + 1}`
        },
        {
          title: '料号',
          dataIndex: 'materialCode',
          // ellipsis: true,
        },
        {
          title: '物料名称',
          dataIndex: 'materialName',
          ellipsis: true,
        },
        {
          title: '图片',
          dataIndex: 'image',
          customRender: (text, record, index) => this.$util.createFcImage(this.$createElement, text)
        },
        {
          title: '规格',
          dataIndex: 'materialModelNumber',
          scopedSlots: {customRender: 'materialModelNumber'}
        },
        {
          title: '采购数量',
          dataIndex: 'number',
        },
        {
          title: '已发数量',
          dataIndex: 'deliveredNumber',
        },
        {
          title: '入库数量',
          dataIndex: 'completedNumber',
        },
        {
          title: '异常数量',
          dataIndex: 'abnormalNumber',
        },
        {
          title: '状态',
          dataIndex: 'executeStatus',
          customRender: (text, record, index) => {
            let es = this.$util.findExecuteStatusById(text)
            return es ? es.name : '未知状态'
          }
        },
        {
          title: '操作',
          width: '100px',
          scopedSlots: {customRender: 'action'}
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return page(Object.assign(parameter, this.queryParam)).then((res) => {
          res.data.rows.forEach(value => {
            value.thisCompleteNumber = 0
          })
          return res.data
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      defaultExpandedKeys: [],
      visible: false,
      confirmLoading: false,
      emsOrder: null,
    }
  },
  methods: {
    show(record) {
      this.visible = true
      this.emsOrder = record
      this.queryParam.orderId = this.emsOrder.orderId
      this.queryParam.materialType = this.$businessEnum.MATERIAL_TYPE_ENUM.material.id
      this.queryParam.pageSize = 999
      setTimeout(() => {
        this.handleOk()
      }, 100)
    },
    handleOk() {
      this.$refs.table.refresh()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    handleSubmit() {
    },
    handleCancel() {
      this.visible = false
    },
  }
}
</script>

<style lang="less">
.table-operator {
  margin-bottom: 18px;
}

button {
  margin-right: 8px;
}
</style>
